<!DOCTYPE html>
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>SSE 推送消息</title>
</head>
<body>
<div id="msgFrompPush">
</div>

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
    /*EventSource 是 SSE 客户端，查看是否支持*/
    if (!!window.EventSource) {
        var source = new EventSource('push');
        s='';
        source.addEventListener('message', function(e) {
            s+=e.data+"<br/>";
            $("#msgFrompPush").html(s);
        });
        source.addEventListener('open', function (e) {
            console.log('连接打开');
        }, false);
        source.addEventListener('error', function (e) {
            if (e.readyState == EventSource.CLOSED) {
                console.log("连接关闭");
            } else {
                console.log(e.readyState);
            }
        }, false);
    } else {
        alert("你的浏览器不支持 SSE")
    }
</script>
</body>
</html>
